package com.huawei.codearts.controller;

import com.huawei.codearts.service.MemberService;
import com.huawei.codearts.tools.utils.ResponseUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * JDBC 模式 增删改查
 */

@RestController
@RequestMapping(value = "member")
public class MemberController {

    @RequestMapping(value = "getMember")
    public String getMember(@RequestParam Map<String, Object> params) {
        // @RequestBody 接收json参数
        // @RequestParam 接收get或post参数, 如果get和post同时有参数并且参数名称相同, 则get参数会覆盖post参数
        System.out.println(params);
        System.out.println(params.get("id"));
        System.out.println(params.get("name"));
        int id = Integer.parseInt((String)params.get("id"));

        String result = null;
        try {
            result = ResponseUtils.response(MemberService.getMember(id));
        } catch(Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "getMemberList")
    public String getMemberList(@RequestParam Map<String, Object> params) {
        String result = null;
        try {
            result = ResponseUtils.response(MemberService.getMemberList());
        } catch(Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "useradd")
    public String useradd(@RequestParam Map<String, Object> params) {
        String result = null;
        try {
            boolean res = MemberService.useradd();
            if (res) {
                result = ResponseUtils.ok();
            } else {
                result = ResponseUtils.error("创建失败");
            }
        } catch(Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "update")
    public String update(@RequestParam Map<String, Object> params) {
        int id = Integer.parseInt((String)params.get("id"));
        String result = null;
        try {
            boolean res = MemberService.update(id);
            if (res) {
                result = ResponseUtils.ok();
            } else {
                result = ResponseUtils.error("更新失败");
            }
        } catch(Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "del")
    public String del(@RequestParam Map<String, Object> params) {
        int id = Integer.parseInt((String)params.get("id"));
        String result = null;
        try {
            boolean res = MemberService.del(id);
            if (res) {
                result = ResponseUtils.ok();
            } else {
                result = ResponseUtils.error("删除失败");
            }
        } catch(Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "updateWithTx")
    public String updateWithTx(@RequestParam Map<String, Object> params) {
        // @RequestBody 接收json参数
        // @RequestParam 接收get或post参数, 如果get和post同时有参数并且参数名称相同, 则get参数会覆盖post参数
        int id = Integer.parseInt((String)params.get("id"));
        String result = null;
        try {
            boolean res = MemberService.updateWithTx(id);
            if (res) {
                result = ResponseUtils.ok();
            } else {
                result = ResponseUtils.error("更新失败");
            }
        } catch(Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping(value = "updateWithTx2")
    public String updateWithTx2(@RequestParam Map<String, Object> params) {
        // @RequestBody 接收json参数
        // @RequestParam 接收get或post参数, 如果get和post同时有参数并且参数名称相同, 则get参数会覆盖post参数
        int id = Integer.parseInt((String)params.get("id"));
        String result = null;
        try {
            boolean res = MemberService.updateWithTx2(id);
            if (res) {
                result = ResponseUtils.ok();
            } else {
                result = ResponseUtils.error("更新失败");
            }
        } catch(Exception e) {
            // 处理 Class.forName 错误
            e.printStackTrace();
        }
        return result;
    }

}
